﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StateMachineCompiler
{
	public class State
	{
		private string name;
		private List<Command> actions = new List<Command>();
		private Dictionary<string, Transition> transitions = new Dictionary<string, Transition>();

		public State(string name, params Command[] actions)
		{
			this.name = name;
			this.actions.AddRange(actions);
		}

		public string Name
		{
			get { return this.name; }
		}

        public IEnumerable<Transition> Transitions
		{
			get { return transitions.Values.ToArray(); }
		}

		public void AddTransition(Event @event, State targetState)
		{
			if (targetState == null)
				throw new ArgumentNullException("targetState");

			transitions.Add(@event.Code, new Transition(this, @event, targetState));
		}

		public bool HasTransition(string eventCode)
		{
			return transitions.ContainsKey(eventCode);
		}

		public State GetTargetState(string eventCode)
		{
			return transitions[eventCode].Target;
		}

		public void ExecuteActions(CommandChannel commandsChannel)
		{
			foreach (var c in actions)
				commandsChannel.Send(c.Code);
		}
	}
}
